#!/usr/bin/python
# -*- coding:utf-8 -*-

def checkIndex(key):
	if not isinstance(key,(int, long)):raise TypeError
	if key<0:raise IndexError

class ArithemticSequence(object):
	def __init__(self, start=0, step = 1):
		super(ArithemticSequence, self).__init__()
		self.start = start
		self.step = step
		self.changed={}

	def __getitem__(self,key):
		checkIndex(key)
		try:
			return self.changed[key]
		except Exception as e:
			return self.start + key*self.step

	def __setitem__(self, key, value):
		checkIndex(key)
		self.changed[key] = value

'''
s = ArithemticSequence(1, 2)
print s[4]
s[4] = 2
print s[4]
print s[5]
'''
class CountList(list):
	def __init__(self,*args):
		super(CountList, self).__init__(*args)
		self.counter = 0
	def __getitem__(self,index):
		self.counter += 1
		return super(CountList, self).__getitem__(index)

c1 = CountList(range(10))	
print c1
print c1.count, c1.counter
print c1[0]+c1[1]
print c1.counter
print c1.counter	
		
class Rectangle(object):
	def __init__(self):
		super(Rectangle,self).__init__()
		self.width = 0
		self.height = 0
	def setSize(self, size):
		self.width, self.height = size
	def getSize(self):
		return self.width,self.height
	size = property(getSize, setSize)

r = Rectangle()
r.width = 10
r.height = 5
print r.size
r.size=(150, 100)
print r.width

class Animals(object):
	def __init__(self):
		super(Animals, self).__init__()
		self.name = 'wa'
		self.age = 10
		self.action = 'run, jump'
	def setDetail(self,content):
		self.name, self.age, self.action = content
	def getDetail(self):
		return self.name+'+ppo', self.action+'+jump', self.age
	Detail = property(getDetail,setDetail)

an = Animals()
an.Detail = ('wwp', 10, 'jump')
print an.Detail


class MyClass(object):
	'''静态方法和类成员方法 '''
	def __init__(self):
		super(MyClass, self).__init__()
	def smeth():
		print 'this is a static method'
	smeth = staticmethod(smeth)
	def cmeth(cls):
		print 'this is a class method of',cls
	cmeth = classmethod(cmeth)

		
class NewMyClass(object):
	'''装饰器静态方法和类成员方法'''
	@classmethod
	def cmeth(cls):
		print 'this is a class method of',cls

	@staticmethod
	def smeth():
		print 'this is a static method'

	def __init__(self):
		super(NewMyClass, self).__init__()


MyClass.smeth()
MyClass.cmeth()
NewMyClass.cmeth()
NewMyClass.smeth()

class NRectangle(object):
	def __init__(self):
		super(NRectangle, self).__init__()
		self.width = 0
		self.height = 0
	def __setattr__(self,name,value):
		if name == 'size':
			self.width,self.height = value
		else:
			self.__dict__[name] = value
	def __getattr__(self,name):
		if name == 'size':
			return self.width, self.height
		else:
			raise AttributeError
		
class Fibs(object):
	'''迭代器'''
	def __init__(self):
		super(Fibs,self).__init__()
		self.a = 0
		self.b = 1
	def next(self):
		self.a, self.b = self.b,self.a+self.b
		return self.a
	def __iter__(self):
		return self

fibs = Fibs()
for f in fibs:
	if f>1000:
		print f
		break

class TestIterator(object):
	value = 0
	def __init__(self):
		super(TestIterator, self).__init__()

	def next(self):
		self.value += 1
		if self.value>10: raise StopIteration
		else:return self.value

	def __iter__(self):
		return self

ti = TestIterator()
print list(ti)
		
# 生成器
mested = [[1,2], [3,4],[5]]		
def flatten(nested):
	for sublist in nested:
		for element in sublist:
			yield element

for num in flatten(mested):
	print num

print list(flatten(mested))









		